# -*- coding: utf-8 -*-
# 给出一个链表，每 k 个节点一组进行翻转，并返回翻转后的链表
# k 是一个正整数，它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍，那么将最后剩余节点保持原有顺序
# 示例 :
# 给定这个链表：1->2->3->4->5
# 当 k = 2 时，应当返回: 2->1->4->3->5
# 当 k = 3 时，应当返回: 3->2->1->4->5

# 说明 :
# 你的算法只能使用常数的额外空间
# 你不能只是单纯的改变节点内部的值，而是需要实际的进行节点交换
def printList(head):
    current = head;
    v_log = [];
    while current:
        v_log.append(current.val);
        current = current.next;
    print v_log;

def printListNodes(nodes):
    v_log = [];
    for node in nodes:
        if not node:
            v_log.append("#");
        else:
            v_log.append(node.val);
    print v_log;

# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

# 使用数组继续需要交换的前后节点
# class Solution(object):
#     def reverseKGroup(self, head, k):
#         """
#         :type head: ListNode
#         :type k: int
#         :rtype: ListNode
#         """
#         if k <= 1:
#             return head;

#         before = [None] * (k + 1);
#         after = [None] * (k + 1);
#         hhead = ListNode(-1);
#         hhead.next = head;

#         current = hhead;
#         while current:
#             break_flag = False;
#             # 收集需要交换的元素
#             for i in xrange(0, k + 1):
#                 if not current:
#                     break_flag = True;
#                     break;
#                 before[i] = current;
#                 after[(i + 2) % (k + 1)] = current.next;
#                 current = current.next;
#             if break_flag:
#                 break;
#             print "#";
#             printListNodes(before);
#             printListNodes(after);
#             for i in xrange(0, k + 1):
#                 before[i].next = after[i];
#             current = before[1];
#             for i in xrange(0, k + 1):
#                 before[i] = None;
#                 after[i] = None;
#         return hhead.next;






# # 阅读最快的解决方案，使用了栈的数据结构
# class Solution(object):
#     def reverseKGroup(self, head, k):
#         """
#         :type head: ListNode
#         :type k: int
#         :rtype: ListNode
#         """
#         if k <= 1 or not head:
#             return head;

#         hhead = ListNode(-1);
#         hhead.next = head;
#         current = hhead;

#         stack = [];
#         num = 0
#         while num < k and head:
#             stack.append(head);
#             head = head.next;
#             num += 1;

#         if num < k:
#             return hhead.next;

#         while len(stack) > 0:
#             current.next = stack.pop();
#             current = current.next;

#         current.next = self.reverseKGroup(head, k);
#         return hhead.next;


# 不使用递归，把算法放在一个方法中完成
# 阅读最快的解决方案，使用了栈的数据结构
class Solution(object):
    def reverseKGroup(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        if k <= 1 or not head:
            return head;

        hhead = ListNode(-1);
        hhead.next = head;
        current = hhead;

        while head:
            stack = [];
            num = 0
            while num < k and head:
                stack.append(head);
                head = head.next;
                num += 1;

            if num < k:
                current.next = stack[0];
                break;

            # 加上这一句，避免在[1, 2], 2这样的输入时，输出环形链表的bug
            stack[0].next = None;
            while len(stack) > 0:
                current.next = stack.pop();
                current = current.next;
        return hhead.next;



l = ListNode(1);
l.next = ListNode(2);
# l.next.next = ListNode(3);
# l.next.next.next = ListNode(4);
# l.next.next.next.next = ListNode(5);
t = Solution();
printList(t.reverseKGroup(l, 2));